package com.cocol.core.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

import com.cocol.core.ioc.ViewInjectUtils;

/**
 * @author Cocol
 * @description 公共Activity
 * @date 15/12/29 下午2:18
 * @email 617882976@qq.com
 */
public abstract class BaseActivity extends AppCompatActivity {

    public String TAG = "BaseActivity";
    public final String ToActivity = "ToActivity";
    public String SUB_TAG = "";
    private Activity mContext;

    public Context getContext() {
        return mContext;
    }

    public Activity getActivity() {
        return mContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        beforeSetView();
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        SUB_TAG = getClass().getSimpleName();
        mContext = this;
        AppActivityManager.getAppManager().addActivity(this);
        ViewInjectUtils.inject(this);
        initTitleBar();
        init(savedInstanceState);
    }

    /**
     * 初始化标题
     */
    protected abstract void initTitleBar();

    /**
     * 在setContentView findView之前
     */
    protected void beforeSetView() {
    }

    /**
     * 相当onCreate方法
     * @param savedInstanceState 保存的实例
     */
    protected abstract void init(Bundle savedInstanceState);

    protected void replace(int holderId, Fragment fragment){
        getSupportFragmentManager().beginTransaction()
                .replace(holderId, fragment).commitAllowingStateLoss();
    }
}
